//mycrops
const router = require('koa-router')()

router.prefix('/crop')
//删除item
router.post('/delcrop', async (ctx, next) => {
  let id = ctx.request.body.id;
  //在数据库中删除对应数据
  await ctx.db.EXCUTE(`DELETE FROM mycrops WHERE id=${id}`)

  //获取删除后的全部数据
  let result = await ctx.db.EXCUTE('SELECT * FROM mycrops ORDER BY id');
  ctx.body = {
    code: 200,
    data: result
  };
})

//页面初始化时获取数据库内的数据条目
router.post('/getcrop', async (ctx, next) => {
  let resultuserid = await ctx.db.EXCUTE("SELECT (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM mycrops n")
  let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT cropname FROM tags ni WHERE n.cropid=ni.cropsid) AS cropinfo FROM mycrops n")

  for (let i = 0; i < result.length; i++) {
    Object.assign(result[i], resultuserid[i]);
  }

  ctx.body = {
    code: 200,
    data: result
  };
})

router.post('/getmycrops', async (ctx, next) => {
  let userid = ctx.request.body.userid
  console.log(typeof (userid));
  let sql = 'SELECT * FROM mycrops where userid=? ORDER BY id'
  let result = await ctx.db.EXCUTE(sql, [userid])
  ctx.body = {
    code: 200,
    data: result
  };
})
router.post('/getnowgrow', async (ctx, next) => {
  let userid = ctx.request.body.userid
  let sql1 = 'SELECT * FROM mycrops where userid=? and down=0 ORDER BY id'
  let result1 = await ctx.db.EXCUTE(sql1, [userid])
  ctx.body = {
    code: 200,
    data: result1,
    nowgrow: result1.length
  };
})
//生长记录recordlist
//页面初始化时获取数据库内的数据条目
router.post('/getrecordlist', async (ctx, next) => {
  let result = await ctx.db.EXCUTE('SELECT * FROM recordlist ORDER BY id');
  ctx.body = result;
})

//删除生长记录表item
router.post('/delrecordlist', async (ctx, next) => {
  let id = ctx.request.body.id;

  //在数据库中删除对应数据
  await ctx.db.EXCUTE(`DELETE FROM recordlist WHERE id=${id}`)
  //WHERE的筛选条件必须是主键

  //获取删除后的全部数据
  let result = await ctx.db.EXCUTE('SELECT * FROM recordlist ORDER BY id');
  ctx.body = result;
})


router.post('/getallmycrops', async (ctx, next) => {
  let userid = ctx.request.body.userid
  // let sql = 'SELECT * FROM mycrops where userid=? ORDER BY id'
  let sql = "select * from mycrops inner join growtime on mycrops.cropid = growtime.cropsid and mycrops.nowgrownum=growtime.grownum inner join background on mycrops.bgplace = background.bgplace and background.bgweather=1 inner join tags on mycrops.cropid = tags.cropsid where userid=? "

  let result = await ctx.db.EXCUTE(sql, [userid])
  let arr = [];
  for (let i = 0; i < result.length; i++) {
    arr.push(result[i].cropname)
    // console.log(result[i].bgplace);
  }
  // console.log(arr);
  // let result1 = await ctx.db.EXCUTE(sql1,[userid])
  // console.log(result);
  ctx.body = {
    code: 200,
    data: result,
    cropname: arr
  };
})
router.post('/getmycropname', async (ctx, next) => {
  let userid = ctx.request.body.userid
  // console.log(typeof (userid));
  // let sql = 'SELECT * FROM mycrops where userid=? ORDER BY id'
  let sql = "select * from mycrops inner join tags on mycrops.cropid = tags.cropsid inner join background on mycrops.bgplace = background.bgplace and background.bgweather=1 where userid=?"

  let result = await ctx.db.EXCUTE(sql, [userid])
  // console.log(result);
  // let result1 = await ctx.db.EXCUTE(sql1,[userid])
  ctx.body = {
    code: 200,
    data: result
  };
})
router.post('/selecttags', async (ctx, next) => {
  let croptype = ctx.request.body.croptype;
  let sql = 'SELECT * FROM tags where croptype=?'
  let result = await ctx.db.EXCUTE(sql, [croptype]);
  // console.log(croptype);
  // console.log(result);
  if (result[0]) {
    ctx.body = {
      code: 200,
      data: result
    };
  }
})

router.post('/updategrowth', async (ctx, next) => {
  let id = ctx.request.body.id;
  let growth = ctx.request.body.growth;
  console.log(ctx.request.body);
  let sql = 'update mycrops set growth=? where id=?'
  let result = await ctx.db.EXCUTE(sql, [growth + 2, id]);
  let sql1 = "select growth from mycrops where id=?"
  let result1 = await ctx.db.EXCUTE(sql1, [id]);
  // if (result[0]) {
  ctx.body = {
    code: 200,
    data: result1
  };
  // }
})
router.post('/updategrowth0', async (ctx, next) => {
  let id = ctx.request.body.id;
  let growth = ctx.request.body.growth;
  console.log(ctx.request.body);
  let sql = 'update mycrops set growth=? where id=?'
  let result = await ctx.db.EXCUTE(sql, [growth - 3, id]);
  let sql1 = "select growth from mycrops where id=?"
  let result1 = await ctx.db.EXCUTE(sql1, [id]);
  // if (result[0]) {
  ctx.body = {
    code: 200,
    data: result1
  };
  // }
})

router.post('/downgrowth', async (ctx, next) => {
  let id = ctx.request.body.id;
  let growth = ctx.request.body.growth;
  console.log(ctx.request.body);
  let sql = 'update mycrops set growth=? where id=?'
  let result = await ctx.db.EXCUTE(sql, [growth - 5, id]);
  let sql1 = "select growth from mycrops where id=?"
  let result1 = await ctx.db.EXCUTE(sql1, [id]);
  // if (result[0]) {
  ctx.body = {
    code: 200,
    data: result1
  };
  // }
})

router.post('/findgrowth', async (ctx, next) => {
  let id = ctx.request.body.id;
  let sql1 = "select growth from mycrops where id=?"
  let result1 = await ctx.db.EXCUTE(sql1, [id]);
  ctx.body = {
    code: 200,
    data: result1
  };

})

// 更新背景图并存储用户选择的背景
router.post('/updatebg', async (ctx, next) => {
  let id = ctx.request.body.id;
  let bgplace = ctx.request.body.bgplace;
  console.log(ctx.request.body);
  let sql1 = "update mycrops set bgplace=? where userid=?"
  let result1 = await ctx.db.EXCUTE(sql1, [bgplace, id]);
  ctx.body = {
    code: 200,
    data: result1
  };

})

router.post('/tip', async (ctx, next) => {
  let cropsid = ctx.request.body.cropsid;
  // console.log(ctx.request.body);
  console.log(1111111122222222222222);
  let sql1 = "select * from growtip where cropsid=?"
  let result1 = await ctx.db.EXCUTE(sql1, [cropsid]);
  // console.log(result1);
  ctx.body = {
    code: 200,
    data: result1
  };

})
router.post('/addcrops', async (ctx, next) => {
  let data = ctx.request.body
  console.log(ctx.request.body);
  let sql = "select bgplace from mycrops where userid=?"
  let result = await ctx.db.EXCUTE(sql, [data.userid]);
  console.log(result);
  if (result[0]) {
    let sql1 = "insert into mycrops (userid, cropid, growth, bgplace, down, nowgrownum, nowgrowday) values (?, ?, ?, ?, ?, ?, ?)"
    let result1 = await ctx.db.EXCUTE(sql1, [data.userid, data.cropid, 60, result[0].bgplace, 0, 1, 1]);
    ctx.body = {
      code: 200,
      data: result1
    };
  }
  else {
    let sql1 = "insert into mycrops (userid, cropid, growth, bgplace, down, nowgrownum, nowgrowday) values (?, ?, ?, ?, ?, ?, ?)"
    let result1 = await ctx.db.EXCUTE(sql1, [data.userid, data.cropid, 60, 2, 0, 1, 1]);
    ctx.body = {
      code: 200,
      data: result1
    };
  }

})

router.post('/updatenow', async (ctx, next) => {
  let userid = ctx.request.body.userid
  console.log(ctx.request.body);
  let day = new Date().getTime();
  // console.log(Math.floor(day / (1000 * 60 * 60 * 24)));
  let sql = "select * from mycrops inner join growtime on mycrops.cropid = growtime.cropsid where userid=?"
  let result = await ctx.db.EXCUTE(sql, [userid]);
  let arr = []
  console.log(result.length);
  for (let i = 0; i < result.length; i = i + 4) {
    // console.log(i);
    // 计算该植物已经种植了多少天
    let distance = Math.floor((day - result[i].startday) / (1000 * 60 * 60 * 24))
    // console.log(distance);
    // console.log(result[i]);
    // 如果时间比该植物整个生长周期长
    let n = parseInt(distance / result[i].growday) + 1
    let m = distance % result[i].growday
    console.log(i, n, m);
    if (n <= 4 && m < result[i].growday && result[i].growth>60) {  // 在1-3阶段，且非该阶段最后一天
      let r = await ctx.db.EXCUTE("update mycrops set nowgrowday=?, nowgrownum=? where id=?", [m, n, result[i].id]);
      // break;
    }
    else if (n < 4 && m == result[i].growday && result[i].growth>60) {  // 在1-3阶段，且为该阶段最后一天
      let r = await ctx.db.EXCUTE("update mycrops set nowgrowday=?, nowgrownum=? where id=?", [1, n + 1, result[i].id]);
      // break;
    } else if (n == 4 && m == growday && result[i].growth>60) {
      let r = await ctx.db.EXCUTE("update mycrops set down=? where id=?", [1, result[i].id]);
      // break;
    }
  }
  ctx.body = {
    code: 200
  }

})

// 用户删除自己的一种作物
router.post('/delone', async (ctx, next) => {
  let id = ctx.request.body.id;
  //在数据库中删除对应数据
  await ctx.db.EXCUTE(`DELETE FROM mycrops WHERE id=${id}`)

  ctx.body = {
    code: 200
  };
})
module.exports = router
